package com.bamboo.eduservice.controller;


import com.bamboo.commonutils.ResponseEntity;
import com.bamboo.eduservice.entity.EduChapter;
import com.bamboo.eduservice.entity.vo.ChapterVo;
import com.bamboo.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jack.wu
 * @since 2020-07-05
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Resource
    private EduChapterService chapterService;

    @ApiOperation(value = "根据课程号查询章节和小节")
    @GetMapping("getChapterVideo/{courseId}")
    public ResponseEntity getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        return ResponseEntity.ok().data("chapterVideoList", chapterVideoList);
    }

    @ApiOperation(value = "根据id查询章节")
    @GetMapping("info/{id}")
    public ResponseEntity getChapterInfo(@PathVariable String id) {
        EduChapter chapter = chapterService.getById(id);
        return ResponseEntity.ok().data("chapter", chapter);
    }

    @ApiOperation(value = "添加章节信息")
    @PostMapping("add")
    public ResponseEntity addChapterInfo(@RequestBody EduChapter chapter) {
        chapterService.save(chapter);
        return ResponseEntity.ok();
    }

    @ApiOperation(value = "修改章节信息")
    @PutMapping("update")
    public ResponseEntity updateChapterInfo(@RequestBody EduChapter chapter) {
        chapterService.updateById(chapter);
        return ResponseEntity.ok();
    }


    @ApiOperation(value = "删除章节")
    @DeleteMapping("delete/{id}")
    public ResponseEntity deleteChapterInfo(@PathVariable String id) {
        boolean isDel = chapterService.deleteChapterInfo(id);
        if (isDel) {
            return ResponseEntity.ok();
        }
        return ResponseEntity.error();
    }


}

